package io.babystar.modules.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;

import io.babystar.modules.business.dao.ShopDao;
import io.babystar.modules.business.entity.ShopEntity;
import io.babystar.modules.business.service.ShopService;


@Service("shopService")
public class ShopServiceImpl extends ServiceImpl<ShopDao, ShopEntity> implements ShopService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShopEntity> page = this.page(
                new Query<ShopEntity>().getPage(params),
                new QueryWrapper<ShopEntity>().eq("business_id",params.get("business_id"))
        );

        return new PageUtils(page);
    }

    @Override
    public List<ShopEntity> selectList(Map<String, Object> params) {
        return this.baseMapper.selectListCus(params);
    }

    @Override
    public PageUtils financeByBistribution(Map<String, Object> params) {
        Page<ShopEntity> page = new Page<>();
        Integer count = this.baseMapper.financeByBistributionCount(params);
        page.setTotal(count);
        Integer pageNuber = Integer.parseInt(params.get("page").toString());
        Integer pageSize = Integer.parseInt(params.get("limit").toString());
        page.setCurrent(pageNuber);
        page.setSize(pageSize);
        Long current = page.getCurrent();
        Long size = page.getSize();
        Long currentStart = size * current - size;
        params.put("currentStart", currentStart);
        params.put("size", size);
        page.setRecords(this.baseMapper.financeByBistribution(params));

        return new PageUtils(page);
    }

    @Override
    public PageUtils financeByShopAndTeacher(Map<String, Object> params) {
        Page<ShopEntity> page = new Page<>();
        Integer count = this.baseMapper.financeByShopAndTeacherCount(params);
        page.setTotal(count);
        Integer pageNuber = Integer.parseInt(params.get("page").toString());
        Integer pageSize = Integer.parseInt(params.get("limit").toString());
        page.setCurrent(pageNuber);
        page.setSize(pageSize);
        Long current = page.getCurrent();
        Long size = page.getSize();
        Long currentStart = size * current - size;
        params.put("currentStart", currentStart);
        params.put("size", size);
        page.setRecords(this.baseMapper.financeByShopAndTeacher(params));

        return new PageUtils(page);
    }

    @Override
    public PageUtils financeDetailedByShopAndTeacher(Map<String, Object> params) {
        Page<ShopEntity> page = new Page<>();
        Integer count = this.baseMapper.financeDetailedByShopAndTeacherCount(params);
        page.setTotal(count);
        Integer pageNuber = Integer.parseInt(params.get("page").toString());
        Integer pageSize = Integer.parseInt(params.get("limit").toString());
        page.setCurrent(pageNuber);
        page.setSize(pageSize);
        Long current = page.getCurrent();
        Long size = page.getSize();
        Long currentStart = size * current - size;
        params.put("currentStart", currentStart);
        params.put("size", size);
        page.setRecords(this.baseMapper.financeDetailedByShopAndTeacher(params));

        return new PageUtils(page);
    }

}